/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DB;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author deyaju
 */
public class DbRegHacienda {
    Db db;
    public int key = 0;
    public DbRegHacienda(String _Query){
        db = new Db();
        if(db.ok == true){
            Insert(_Query);
        }else{
            System.out.println("Error de conexion!");
        }
    }
    public void Insert (String query){
        
        try {
            Statement stmt ;
            stmt = db.c.createStatement();
            String sql =query;
            PreparedStatement prstm;
            prstm = db.c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            prstm.executeUpdate();
            ResultSet keys = prstm.getGeneratedKeys();
            keys.next();
            key = keys.getInt(1);
            keys.close();
            prstm.close();
            System.out.println(key);
            db.c.commit();
            db.c.close();
        } catch (SQLException ex) {
            Logger.getLogger(DbRegHacienda.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (key>0)
            {
                System.out.println("Operacion exitosa!");
            }
            else
            {
                System.out.println("Operacion fallida!");
            }
        
    }
}
